<?php

/**
 * phImageBuilder form configuration
 *
 * @package    phImageBuilder
 * @subpackage ImageBuilderForm
 * @author     Samusevich Alexander <archer@fishartis.com>
 * @version    1.0 2011-11-14 14:27 archer
 */
class ImageBuilderForm
{
  public function configure( $oObject )
  {               
      // Set default values for variables
      $oObject->sNamespace     = ( isset($oObject->sNamespace) )     ? $oObject->sNamespace     : sfInflector::tableize( get_class( $oObject->getObject() ) );         
      $oObject->sImgField      = ( isset($oObject->sImgField) )      ? $oObject->sImgField      : sfConfig::get('app_'.$oObject->sNamespace.'_image_field', 'photo');
      $oObject->sImgFolder     = ( isset($oObject->sImgFolder) )     ? $oObject->sImgFolder     : sfConfig::get('app_'.$oObject->sNamespace.'_image_folder', $oObject->sNamespace);
      $oObject->sCallbackRoute = ( isset($oObject->sCallbackRoute) ) ? $oObject->sCallbackRoute : 'phImageBuilderWidget';
      $oObject->aThumbs        = ( isset($oObject->aThumbs) )        ? $oObject->aThumbs        : sfConfig::get('app_'.$oObject->sNamespace.'_thumbs');
      
      $sImgMethod = 'get' . sfInflector::camelize( $oObject->sImgField ); 
                                                                               
      // Load Url helper
      sfProjectConfiguration::getActive()->loadHelpers(array('Url'));
      
      // Set phImageBuilderWidget widget
      $widgetSchema = $oObject->getWidgetSchema();
      $widgetSchema[$oObject->sImgField] = new sfWidgetFormInputImageBuilder
      (
            array
            (
                'file_src'     => $oObject->getObject()->getPhotoPath(),
                'edit_mode'    => (bool) $oObject->getObject()->$sImgMethod(), 
                'thumbs'       => $oObject->aThumbs,
                'with_delete'  => false,
                'callback'     => url_for( $oObject->sCallbackRoute, array( 'model' => get_class( $oObject->getObject() ), 'id' => $oObject->getObject()->getId() ) )
            )
      );

      // Enable remove buttom
      $validatorSchema = $oObject->getValidatorSchema();
      $validatorSchema[$oObject->sImgField . '_delete'] = new sfValidatorPass(); 
      
      $validatorSchema[$oObject->sImgField] = new sfValidatorFile
      (
            array( 'required' => false, 'path' => sfConfig::get('sf_upload_dir'). '/' . trim( $oObject->sImgFolder, '/' ), 'mime_types' => 'web_images' )
      );
  }
}
